package com.personal.ifanya.presentation.controller;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.personal.ifanya.application.service.UserService;
import com.personal.ifanya.domain.UserBiz;
import com.personal.ifanya.presentation.model.AsynResult;
import com.personal.ifanya.presentation.model.SessionBox;


@Controller
@RequestMapping("/account")
@SessionAttributes(value={"sessionBox"})
public class UserController {

	
	@Autowired
	private UserService userService;
	
 
	
	@RequestMapping(value="/check", produces = "application/json; charset=utf-8")
	@ResponseBody
	public String checkUser(@ModelAttribute("sessionBox") SessionBox sessionBox, 
			@RequestParam("name") String name, 
			@RequestParam("password") String password,
			HttpServletResponse response)
	{ 
		AsynResult<Boolean> result = new AsynResult<Boolean>();
		if(userService.checkUser(name, password))
		{
			sessionBox.setLogined(true);
			result.setAll(true, true, AsynResult.Error.NONE);
		}
		else
		{
			result.setAll(true, false, AsynResult.Error.FAILED);
		} 
		return JSON.toJSONString(result);
	}
	 
	
}
